package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.query.CustomDataConfigQuery;
import com.ruoyi.common.core.domain.vo.MenuRoleAuthorityVO;
import com.ruoyi.service.SysMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 菜单信息
 */
@RestController("菜单管理")
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuServiceImpl menuService;

    /**
     * 获取菜单列表
     */

    @GetMapping("/list")
    public AjaxResult list(SysMenu menu) {
        List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
        return success(menus);
    }

    @RequestMapping(value = "/v2/list")
    public Object routerGetList(String terminal) throws Exception {
        return success(menuService.routerGetList(terminal));
    }

    /**
     * 获取菜单下拉树列表
     */
    @RequestMapping(value = "/v2/authority/list", name = "获取有权限的菜单下拉树列表")
    public Object authorityMenuList() throws Exception {
        return success(menuService.authorityMenuList());
    }


    /**
     * 获取菜单下拉树列表
     */
    @RequestMapping(value = "/v2/roleAuthority", name = "菜单角色授权")
    public Object roleAuthority(@RequestBody MenuRoleAuthorityVO menuRoleAuthority) throws Exception {
        menuService.roleAuthority(menuRoleAuthority);
        return "";
    }


    /**
     * 根据菜单编号获取详细信息
     */

    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return success(menuService.selectMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysMenu menu) {
        List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
        return success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        List<SysMenu> menus = menuService.selectMenuList(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    @RequestMapping(value = "/router/save")
    public Object routerSave(String json) throws Exception {
        menuService.routerSave(json);
        return success();

    }

    @RequestMapping(value = "/doEdit")
    public Object menuManagementDoEdit(@RequestBody SysMenu action) {
        action.setVersion("v2");
        menuService.doSave(action);
        return success();
    }

    @RequestMapping(value = "/doDelete")
    public Object doDelete(SysMenu action) {
        menuService.doDelete(action);

        return success();
    }


    @RequestMapping(value = "/doCopy")
    public Object doCopy(SysMenu action) {
        menuService.doCopy(action);
        return success();
    }


    @RequestMapping(value = "/getAuthorityMenuList")
    public Object getAuthorityMenuList(String terminal) throws Exception {
        return success(menuService.getAuthorityMenuList(terminal));
    }


    @RequestMapping(value = "/getMenuListForRoleAuthority")
    public Object getMenuListForRoleAuthority() throws Exception {
        return success(menuService.getMenuListForRoleAuthority());
    }


    @RequestMapping(value = "/getInfo")
    public Object menuManagementInfo(Long menuId) {
        SysMenu action = menuService.getInfo(menuId);
        return success(action);
    }

    @RequestMapping(value = "doSave")
    public Object doSave(@RequestBody CustomDataConfigQuery vo) {
        menuService.doSaveMeta(vo);
        return "";
    }


}